<?php

namespace App\Http\Controllers\Admin;

use App\Models\Guide;
use App\Models\GuidePage;

class GuidePageController extends Controller
{
    /*
     * @api get /course-api/a/guides/{guideId}/pages 精彩绘本页面列表
     * @version 0.0.1
     * @group admin
     *
     * @param path int guideId 精彩绘本ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "items":[]
     * }
     * }
     */
    public function index($guideId)
    {
        $pages = GuidePage::where('guide_id', $guideId)->orderBy('sequence', 'asc')->orderBy('id', 'desc')
            ->get(['id', 'name']);
        $items = [];
        /** @var GuidePage $page */
        foreach ($pages as $page) {
            $items[] = [
                'id' => $page->id,
                'name' => $page->getName(),
            ];
        }
        return $this->sendSuccessJson(['items'=>$items]);
    }

    /*
     * @api post /course-api/a/guides/{guideId}/pages 新增精彩绘本页面
     * @version 0.0.1
     * @group admin
     *
     * @param path int guideId 精彩绘本ID
     * @param body array content 内容
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function store($guideId)
    {
        $this->validate($this->request, [
            'name' => 'string',
            'content' => 'array'
        ]);
        $guidePageObj = new GuidePage();
        $guidePageObj->setGuideId($guideId);
        if ($this->request->has('name')) {
            $guidePageObj->setName($this->request->input('name'));
        }
        if ($this->request->has('content')) {
            $guidePageObj->setContent($this->request->input('content'));
        }
        $guidePageObj->save();
        return $this->sendSuccessJson($guidePageObj->getId());
    }

    /*
     * @api put /course-api/a/guides/{guideId}/pages/{pageId} 更新精彩绘本页面
     * @version 0.0.1
     * @group admin
     *
     * @param path int guideId 精彩绘本ID
     * @param path int pageId 页面ID
     * @param body array content 内容
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "items":[]
     * }
     * }
     */
    public function update($guideId, $pageId)
    {
        $this->request->merge(['id'=>$pageId]);
        $this->validate($this->request, [
            'id' => 'required|integer',
            'name' => 'string',
            'content' => 'array',
        ]);
        /** @var GuidePage $guidePageObj */
        $guidePageObj = GuidePage::find($pageId);
        if ($this->request->has('name')) {
            $guidePageObj->setName($this->request->input('name'));
        }
        if ($this->request->has('content')) {
            $guidePageObj->setContent($this->request->input('content'));
        }
        $guidePageObj->save();
        return $this->sendSuccessJson($guidePageObj->getId());
    }

    /*
     * @api get /course-api/a/guides/{guideId}/pages/{pageId} 精彩绘本页面详情
     * @version 0.0.1
     * @group admin
     *
     * @param path int guideId 精彩绘本ID
     * @param path int pageId 页面ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "items":[]
     * }
     * }
     */
    public function show($guideId, $pageId)
    {
        $this->request->merge(['guideId'=>$guideId, 'pageId'=>$pageId]);
        $this->validate($this->request, [
            'guideId' => 'required|integer',
            'pageId' => 'required|integer',
        ]);
        /** @var GuidePage $guidePageObj */
        $guidePageObj = GuidePage::find($pageId);
        $data = [
            'id' => $guidePageObj->id,
            'name' => $guidePageObj->getName(),
            'content' => $guidePageObj->getContent(),
        ];
        return $this->sendSuccessJson($data);
    }

    /*
     * @api delete /course-api/a/guides/{guideId}/pages/{pageId} 删除精彩绘本页面
     * @version 0.0.1
     * @group admin
     *
     * @param path int guideId 精彩绘本ID
     * @param path int pageId 页面ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "items":[]
     * }
     * }
     */
    public function destroy($guideId, $pageId)
    {
        GuidePage::destroy(intval($pageId));
        return $this->sendSuccessJson();
    }
}
